package services;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.SqlSession;

import com.aton.db.SessionFactory;

import models.SystemParams;
import models.mappers.SystemParamMapper;
import play.cache.Cache;
import utils.BizConstants;

public class SystemParamService {
	/**
	 * 根据id获取系统参数
	 *
	 * @Title: getValueById 
	 * @param id
	 * @return
	 * @return: SystemParams
	 */
	public static String getValueById(String id){
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			SystemParamMapper mapper = ss.getMapper(SystemParamMapper.class);
            return mapper.getValueById(id);
        } finally {
            ss.close();
        }
	}
	/**
	 * 获取参数列表
	 *
	 * @Title: listParams 
	 * @return
	 * @return: List<SystemParams>
	 */
	public static List<SystemParams> listParams() {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			SystemParamMapper mapper = ss.getMapper(SystemParamMapper.class);
            return mapper.listParams();
        } finally {
            ss.close();
        }
	}
	/**
	 * 通过缓存查询系统参数
	 *
	 * @Title: getByCacheId 
	 * @param personValidityTerm
	 * @return
	 * @return: SystemParams
	 */
	public static String getByCacheId(String personValidityTerm) {
		String param=(String)Cache.get(BizConstants.SYSTEM_PARAM+personValidityTerm);
		if(StringUtils.isNotEmpty(param)){
			return param;
		}
		else{
			return getValueById(personValidityTerm);
		}
	}
	/**
	 * 保存参数
	 *
	 * @Title: saveParam 
	 * @return: void
	 */
	public static void saveParam(SystemParams param){
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			SystemParamMapper mapper = ss.getMapper(SystemParamMapper.class);
			mapper.saveParam(param);
			ss.commit();
		}catch(Exception e){
			ss.rollback();
			e.printStackTrace();
		}finally{
			
		}
	}
}
